<?php

require_once dirname(__FILE__) . '/../lib/sfGuardUserGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/sfGuardUserGeneratorHelper.class.php';

/**
 * sfGuardUser actions.
 *
 * @package    Amman to London
 * @subpackage sfGuardUser
 * @author     Adel Abu-Saif <adeljas@msn.com>
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sfGuardUserActions extends autoSfGuardUserActions {

    public function executeFilter(sfWebRequest $request) {

        $this->setPage(1);
        $uni_id = $request->getParameter("uni_id");
        $faculty_id = $request->getParameter("faculty_id");
        if ($request->hasParameter('_reset')) {
            $this->setFilters($this->configuration->getFilterDefaults());

            $this->redirect('@sf_guard_user');
        }

        $this->filters = $this->configuration->getFilterForm($this->getFilters());

        if ($uni_id) {

            $this->setFilters(array("university_id" => $uni_id));
            $this->redirect('@sf_guard_user');
        }
        if ($faculty_id) {

            $this->setFilters(array("faculty_id" => $faculty_id));
            $this->redirect('@sf_guard_user');
        }
        $this->filters->bind($request->getParameter($this->filters->getName()));
        if ($this->filters->isValid()) {
            $this->setFilters($this->filters->getValues());

            $this->redirect('@sf_guard_user');
        }

        $this->pager = $this->getPager();
        $this->sort = $this->getSort();

        $this->setTemplate('index');
    }

    public function executeExportCsv(sfWebRequest $req) {
        $res = $this->buildQuery();
        $filters = $this->getFilters();
        if ($filters['faculty_id']) {
            $faculty_name = FacultyTable::getInstance()
                    ->createQuery("f")
                    ->select('f.name_en')
                    ->where("f.id=?", $filters['faculty_id'])
                    ->fetchArray();

            $file_to_faq_name =  str_replace(' ', '_',$faculty_name[0]['name_en']);
        }
        if ($filters['university_id']) {
            $faculty_name = UniversityTable::getInstance()
                    ->createQuery("u")
                    ->select('u.name_en')
                    ->where("u.id=?", $filters['university_id'])
                    ->fetchArray();

            $file_to_faq_name =  str_replace(' ', '_',$faculty_name[0]['name_en']);
        }



     

        if ($file_to_faq_name)
            $file_name = '/tmp/' . $file_to_faq_name. '_' . date("Y-m-d") . '.csv';
        else
            $file_name = '/tmp/usersList_'. date("Y-m-d") . 'csv';

        $f = fopen($file_name, 'w+');
        fputcsv($f, array('User ID', 'First Name', 'Email', 'username'));

        foreach ($res->fetchArray() as $row) {

//            print_r($row); die();
            fputcsv($f, array($row['id'],
                $row['first_name'],
                $row['email_address'],
                $row['username']
            ));
        }
        fclose($f);

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header("Content-Disposition: attachment; filename=" . basename($file_name));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Length: ' . filesize($file_name));
        echo file_get_contents($file_name);

        exit;
    }

}
